package pers.cj.pdos.controller.base;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;
import pers.cj.pdos.controller.entity.IndustryType;
import pers.cj.pdos.controller.entity.ResponseData;
import pers.cj.pdos.entity.*;
import pers.cj.pdos.service.*;
import pers.cj.pdos.utils.*;
import pers.cj.pdos.vo.UserVo;

import java.io.File;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

public abstract class BaseController {

    @Autowired
    protected ICompanyService companyService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IIndustryService industryService;
    @Autowired
    private IBusinessSetService businessSetService;
    @Autowired
    IGoodsCostService goodsCostService;
    @Autowired
    IHouseService houseService;

    /**
     * 获取登录用户所属公司
     * @return
     */
    protected Company getCompany() {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        return companyService.getById(user.getCompanyId());
    }

    /**
     * 获取登录用户的用户id
     * @return
     */
    protected String getLoginUserId() {
        return  ((UserVo) SecurityUtils.getSubject().getPrincipal()).getId();
    }
    /**
     * 获取登录用户的用户名
     * @return
     */
    protected String getLoginUsername() {
        return  ((UserVo) SecurityUtils.getSubject().getPrincipal()).getUsername();
    }


    /**
     * 获取登录用户的用户名
     * @return
     */
    protected boolean isLoginUserAdmin() {
        return  ((UserVo) SecurityUtils.getSubject().getPrincipal()).isAdmin();
    }

    protected UserVo getLoginUserVo() {
        UserVo temp = (UserVo) SecurityUtils.getSubject().getPrincipal();
        UserVo userVo =  userService.getVoById(getLoginUserId());
        userVo.setAdmin(temp.isAdmin());
        return userVo;
    }

    /**
     * 获取登录用户的所属公司的行业
     * @return
     */
    protected Industry getIndustry() {
        return industryService.getById(getCompany().getIndustryId());
    }

    /**
     * 获取登录用户所属公司的业务参数设置
     * @return
     */
    protected BusinessSet getBusinessSet() {
        return businessSetService.getByCompany(getCompany().getId());
    }

    /**
     * 获取 家电数码 对应的行业类型
     * @return
     */
    protected Industry getSnIndustry() {
        return industryService.getByName("家电数码");
    }

    /**
     * 获取当前的类型
     * 1-烟酒食品的话，保质期批次可以出现,sn序列号不可以出现
     * 2-家电数码的话，sn序列号可以出现，保质期批次不可以出现
     * 3-其他行业
     */
    protected IndustryType getIndustryType (){
        Industry industry = getIndustry();
        IndustryType industryType = new IndustryType();
        if (industry.getName().equals("烟酒食品")){
            industryType.setType(1);
        } else if (industry.getName().equals("家电数码")){
            industryType.setType(2);
        } else {
            industryType.setType(3);
        }
        return industryType;
    }

    /**
     * 供外子类调用
     * @param imageFile
     * @param typeName
     * @return
     */
    protected ResponseData processUploadImg(MultipartFile imageFile, String typeName){
        User user = getLoginUserVo();
        try {
            if(imageFile == null){
                return ResponseData.fail("图片不存在");
            }
            // 上传文件大小

            //保存头像的路径
            String path = FileProperUtil.getInstance().getVal("image."+typeName);
            //文件名
            String uploadName= imageFile.getOriginalFilename();
            if (Utils.isEmpty(uploadName)) {
                return ResponseData.fail("图片不存在");
            }
            String type = uploadName.substring(uploadName.lastIndexOf(".")+1);
            String filename = ImgUtil.getFileName(user.getUsername(), type);


            try{
                File dir=new File(path);
                if(!dir.exists()){
                    dir.mkdirs();
                }
                //先把用户上传到原图保存到服务器上
                File file=new File(dir,filename);
                imageFile.transferTo(file);
                // 上传文件成功
                if(file.exists()){
                    //成功返回图片地址
                    return ResponseData.success(filename);
                }
                return ResponseData.fail("图片保存失败");
            }catch (Exception e) {
                return ResponseData.fail("图片保存失败");
            }
        }  catch (Exception e) {
            return ResponseData.fail("图片保存失败");
        }
    }

    protected Map<String,BigDecimal> genCostMap(GoodsUtil goodsUtil) {
        Map<String,BigDecimal> goodsNumberMap = goodsUtil.getAllGoodsNumberMap();
        //计算订单中所有商品的成本价
        Map<String,BigDecimal> map = new HashMap<>();

        for (GoodsCost goodsCost : goodsCostService.listByGoodsMap(goodsNumberMap)){
            map.put(goodsCost.getGoodsId(), goodsCost.getCost());
        }
        return map;
    }

    protected boolean getHouseLocking(String houseId){
        House house =houseService.getById(houseId);
        return house.getLocking()==null ? false:house.getLocking();
    }
}
